/*=========================================================================================================================================

					
					USAGE IN			:		VFS PROJECT, GROUP 31
					
					OPERATION TYPE		:		FILE OPERATION
					
					NAME OF FILE		:		move_file.c
					
					NAME OF OPERATION	:		movefile(P1, P2)
					
												- P1: source file path (in VFS)
												- P2: destination file path (in VFS)
					
					FORMALY CALLED AS	:		movefile(source_file_path,dest_file_path)
					
					VERSION				:		1.0
					
					CREATED BY			:		ANUJ DUGGAL
					
					TESTED BY			:		ANUJ DUGGAL
					
					
					TODO				:		Define a function that accepts two arguments as mentioned above, and does the following:
												
												VALIDATIONS (APPLYING ERROR CODES):
													- mounted, 
													- source file existance (in VFS),
													- destination path existance (in VFS) 
																										
												TASK:
													- Search the source file in N-ARY, BST
													- Update (Change) the locationpath
													- Delete the file from the source file path
													- Insert the file to the destination path
													
					OTHER FUNCTIONS		:		- validateMountState()
					CALLED						- search_nary(source_file_path)
												- search(bst_root, source_file_path)
												- search_nary(dest_file_path)
												- search(bst_root, dest_file_path)
												- delete_bstnode(bst_root,store_FD->locationpath)
												- state=delete_file_from_nary(store_FD->locationpath)
												- insert_nary(store_FD)
												- BSTInsert(store_FD)
												- ht_start= hash_insert(ht_start, filename)


==========================================================================================================================================*/



#include"common_headers.h"
int movefile31( char source_file_path[], char dest_file_path[] ) 
{
	//DECLARATIONS:
    int state, collect=0, is_mounted;
    struct BST_Node *temp;
    struct FileDescriptor *store_FD=NULL;
    char delims[]="/",save_locationpath[300];
    char *result=NULL,*prev_result=NULL;
    char filenameFromfilepath[50], filename[100];
    
   	/*
	
	ASSUMPTIONS:	
		collect=0 IMPLIES THAT "movefile" OPERATION IS NOT ACHIEVED (SOME ERROR OCCURED)
		collect=1 IMPLIES THAT "movefile" OPERATION IS SUCCESSFUL
		
		is_mounted=0 IMPLIES THAT VFS IS NOT MOUNTED
		is_mounted=1 IMPLIES THAT VFS IS MOUNTED		
		
	*/


    //INITIALIZATIONS:
    collect=0;
    is_mounted=0;



    //CHECK IF VFS MOUNTED
    is_mounted = validateMountState();
    if(is_mounted)
    {
    	strcpy(save_locationpath,source_file_path);
		
		//EXTRACT FILENAME FROM SOURCE FILEPATH
		result = strtok(save_locationpath, delims);
		while(result!=NULL)
		{
			strcpy(filenameFromfilepath, result);
			result=strtok(NULL, delims);
		}
		strcpy(filename,filenameFromfilepath);	//FILENAME EXTRACTED
		
		//CHECK IF FILE EXIST IN THE SPECIFIED SOURCE FILE PATH (CHECK IN VFS)
		if(search(bst_root,source_file_path) && search_nary(source_file_path))
		{
			temp = search(bst_root,source_file_path);
			
			//CHECK IF DESTINATION PATH EXIST (CHECK IN VFS)
			if(search(bst_root,dest_file_path) && search_nary(dest_file_path))
			{
				store_FD=temp->data;
				
				//DELETION FROM BST, N-ARY  (NO NEED TO DELETE FROM HASHTABLE)
				delete_bstnode(bst_root,store_FD->locationpath);
				state=delete_file_from_nary(store_FD->locationpath);
				
				//CHECKING IF THE DESINATION PATH HAS '/'. IF NOT, THEN CONCATENATE '/' AT THE END
				if(strcmp(dest_file_path,"/") != 0)
   				{
       				strcat(dest_file_path,"/");
    			}
    			strcat(dest_file_path,filename);
    			
    			//CHANGING THE LOCATION PATH IN THE LOCATION_FULL_PATH IN FILE DESCRIPTOR
    			strcpy(store_FD->locationpath,dest_file_path);
    			
    			//INSERTING THE SAME IN DATA STRUCTURES
    			insert_nary(store_FD);
    			BSTInsert(store_FD);
    		    ht_start= hash_insert(ht_start, filename);
    		    collect = 1;
    		    printf("movefile_SUCCESS\n");    			
			}
			else
			{
				//DESTINATION PATH NOT FOUND
				printf("movefile_FAILURE %s\n",ERR_VFS_MOVEFILE_02);
			}
		}
		else
		{
			//SOURCE FILE NOT FOUND
			printf("movefile_FAILURE %s\n",ERR_VFS_MOVEFILE_01);
		}
    }
    else
    {
   		//VFS NOT MOUNTED
    	printf("movefile_FAILURE %s\n",ERR_VFS_MOVEFILE_06);
    }
    return collect;
}

